package com.hdu.point_offer;

public class lcr162 {
    class Solution {
        public int digitOneInNumber(int num) {
            return f(num, 1);
        }

        int f(int num, int digit)
        {
            int ans = 0;
            for (int cur = 0, left = 0, right = 1, tmp = num; tmp != 0; tmp /= 10, right *= 10)
            {
                left = tmp / 10;
                cur = tmp % 10;
                if (digit == 0) left--;
                ans += left * right;
                if (cur > digit) ans += right;
                else if (cur == digit) ans += num % right + 1;
            }
            return ans;
        }
    }
}
